package com.innovate.nms.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeoutException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.innovate.nms.common.Ping;
import com.innovate.nms.service.command.Echo;
import com.innovate.nms.service.command.Proxy;
import com.ivan.toolkit.swing.ShadowLabel;

public class DebugPanel extends JPanel implements Ping.Printer{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8910896759133881885L;
	private JTextField tfEmuIP;
	private JTextField tfEmuSms;
	private JTextField tfTargetIP;
	private JTextArea textArea;
	
	private ExecutorService executor = Executors.newSingleThreadExecutor();

	/**
	 * Create the panel.
	 */
	public DebugPanel() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{20, 0, 179, 0, 0, 0, 0, 20, 0};
		gridBagLayout.rowHeights = new int[]{20, 0, 0, 0, 0, 0, 10, 0, 20, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		ShadowLabel shadowLabel = new ShadowLabel();
		shadowLabel.setForeground(new Color(51, 102, 0));
		shadowLabel.setFont(new Font("SansSerif", Font.BOLD, 24));
		shadowLabel.setShadow(new Color(51, 153, 153));
		shadowLabel.setText("功能调试");
		GridBagConstraints gbc_shadowLabel = new GridBagConstraints();
		gbc_shadowLabel.gridwidth = 6;
		gbc_shadowLabel.insets = new Insets(0, 0, 5, 5);
		gbc_shadowLabel.gridx = 1;
		gbc_shadowLabel.gridy = 1;
		add(shadowLabel, gbc_shadowLabel);
		
		JLabel lblEmuIp = new JLabel("EMU  IP：");
		GridBagConstraints gbc_lblEmuIp = new GridBagConstraints();
		gbc_lblEmuIp.anchor = GridBagConstraints.EAST;
		gbc_lblEmuIp.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmuIp.gridx = 1;
		gbc_lblEmuIp.gridy = 3;
		add(lblEmuIp, gbc_lblEmuIp);
		
		tfEmuIP = new JTextField();
		GridBagConstraints gbc_tfEmuIP = new GridBagConstraints();
		gbc_tfEmuIP.insets = new Insets(0, 0, 5, 5);
		gbc_tfEmuIP.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfEmuIP.gridx = 2;
		gbc_tfEmuIP.gridy = 3;
		add(tfEmuIP, gbc_tfEmuIP);
		tfEmuIP.setColumns(10);
		
		JButton btnPing = new JButton("PING");
		btnPing.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				executor.submit(new Runnable() {

					@Override
					public void run() {
						Ping.ping(tfEmuIP.getText(), 2000, DebugPanel.this);	
					}
										
				});
			}
		});
		GridBagConstraints gbc_btnPing = new GridBagConstraints();
		gbc_btnPing.insets = new Insets(0, 0, 5, 5);
		gbc_btnPing.gridx = 3;
		gbc_btnPing.gridy = 3;
		add(btnPing, gbc_btnPing);
		
		JButton btnCheck = new JButton("CHECK");
		btnCheck.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				executor.submit(new Runnable() {

					@Override
					public void run() {
						String target = tfEmuIP.getText();
						doPrint("\r\n正在通过UDP通道检测： " + target );
						try {
							Echo checker = new Echo();
							checker.setTarget(target);
							checker.start("UDP");
							doPrint("检测成功");
						} catch(TimeoutException e) {
							doPrint("检测失败：超时");
						} catch(RuntimeException e) {
							doPrint("检测失败：" + e.getMessage());
						}	
					}
										
				});

			}
		});
		GridBagConstraints gbc_btnCheck = new GridBagConstraints();
		gbc_btnCheck.insets = new Insets(0, 0, 5, 5);
		gbc_btnCheck.gridx = 4;
		gbc_btnCheck.gridy = 3;
		add(btnCheck, gbc_btnCheck);
		
		JButton btnChkTarget = new JButton("CHK TARGET");
		btnChkTarget.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				executor.submit(new Runnable() {

					@Override
					public void run() {
						String emuIP = tfEmuIP.getText();
						String rtuIP = tfTargetIP.getText();
						doPrint("\r\n正在通过UDP通道，经EMU["+emuIP+"]检测： " + rtuIP );
						try {
							Proxy checker = new Proxy();
							checker.setProxy(emuIP);
							checker.setTarget(rtuIP);
							checker.start("UDP");
							if(checker.isAttachable())
								doPrint("检测完成，EMU检测RTU成功");
							else
								doPrint("检测完成，EMU检测RTU失败");
						} catch(TimeoutException ex) {
							doPrint("检测失败：超时");
						} catch(RuntimeException ex) {
							doPrint("检测失败：" + ex.getMessage());
						}	
					}
										
				});

			}
		});
		GridBagConstraints gbc_btnChkTarget = new GridBagConstraints();
		gbc_btnChkTarget.insets = new Insets(0, 0, 5, 5);
		gbc_btnChkTarget.gridx = 5;
		gbc_btnChkTarget.gridy = 3;
		add(btnChkTarget, gbc_btnChkTarget);
		
		JLabel lblEmuSms = new JLabel("EMU SMS：");
		GridBagConstraints gbc_lblEmuSms = new GridBagConstraints();
		gbc_lblEmuSms.anchor = GridBagConstraints.EAST;
		gbc_lblEmuSms.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmuSms.gridx = 1;
		gbc_lblEmuSms.gridy = 4;
		add(lblEmuSms, gbc_lblEmuSms);
		
		tfEmuSms = new JTextField();
		GridBagConstraints gbc_tfEmuSms = new GridBagConstraints();
		gbc_tfEmuSms.insets = new Insets(0, 0, 5, 5);
		gbc_tfEmuSms.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfEmuSms.gridx = 2;
		gbc_tfEmuSms.gridy = 4;
		add(tfEmuSms, gbc_tfEmuSms);
		tfEmuSms.setColumns(10);
		
		JButton btnCheck2 = new JButton("CHECK");
		btnCheck2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				executor.submit(new Runnable() {

					@Override
					public void run() {
						String target = tfEmuSms.getText();
						doPrint("\r\n正在通过SMS通道检测： " + target );
						try {
							Echo checker = new Echo();
							checker.setTarget(target);
							checker.start("SMS");
							doPrint("检测成功");
						} catch(TimeoutException e) {
							doPrint("检测失败：超时");
						} catch(RuntimeException e) {
							doPrint("检测失败：" + e.getMessage());
						}	
					}
										
				});

			}
		});
		GridBagConstraints gbc_btnCheck2 = new GridBagConstraints();
		gbc_btnCheck2.insets = new Insets(0, 0, 5, 5);
		gbc_btnCheck2.gridx = 4;
		gbc_btnCheck2.gridy = 4;
		add(btnCheck2, gbc_btnCheck2);
		
		JButton btnChkTarget2 = new JButton("CHK TARGET");
		btnChkTarget2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				executor.submit(new Runnable() {

					@Override
					public void run() {
						String emuSMS = tfEmuSms.getText();
						String rtuIP = tfTargetIP.getText();
						doPrint("\r\n正在通过SMS通道，经EMU["+emuSMS+"]检测： " + rtuIP );
						try {
							Proxy checker = new Proxy();
							checker.setProxy(emuSMS);
							checker.setTarget(rtuIP);
							checker.start("SMS");
							if(checker.isAttachable())
								doPrint("检测完成，EMU检测RTU成功");
							else
								doPrint("检测完成，EMU检测RTU失败");
						} catch(TimeoutException ex) {
							doPrint("检测失败：超时");
						} catch(RuntimeException ex) {
							doPrint("检测失败：" + ex.getMessage());
						}	
					}
										
				});

			}
		});
		GridBagConstraints gbc_btnChkTarget2 = new GridBagConstraints();
		gbc_btnChkTarget2.insets = new Insets(0, 0, 5, 5);
		gbc_btnChkTarget2.gridx = 5;
		gbc_btnChkTarget2.gridy = 4;
		add(btnChkTarget2, gbc_btnChkTarget2);
		
		JLabel lblTargetIp = new JLabel("Target IP：");
		GridBagConstraints gbc_lblTargetIp = new GridBagConstraints();
		gbc_lblTargetIp.anchor = GridBagConstraints.EAST;
		gbc_lblTargetIp.insets = new Insets(0, 0, 5, 5);
		gbc_lblTargetIp.gridx = 1;
		gbc_lblTargetIp.gridy = 5;
		add(lblTargetIp, gbc_lblTargetIp);
		
		tfTargetIP = new JTextField();
		GridBagConstraints gbc_tfTargetIP = new GridBagConstraints();
		gbc_tfTargetIP.insets = new Insets(0, 0, 5, 5);
		gbc_tfTargetIP.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfTargetIP.gridx = 2;
		gbc_tfTargetIP.gridy = 5;
		add(tfTargetIP, gbc_tfTargetIP);
		tfTargetIP.setColumns(10);
		
		JButton btnPing_1 = new JButton("PING");
		btnPing_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				executor.submit(new Runnable() {

					@Override
					public void run() {
						Ping.ping(tfTargetIP.getText(), 2000, DebugPanel.this);	
					}
										
				});
				
			}
		});
		GridBagConstraints gbc_btnPing_1 = new GridBagConstraints();
		gbc_btnPing_1.insets = new Insets(0, 0, 5, 5);
		gbc_btnPing_1.gridx = 3;
		gbc_btnPing_1.gridy = 5;
		add(btnPing_1, gbc_btnPing_1);
		
		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 6;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 7;
		add(scrollPane, gbc_scrollPane);
		
		textArea = new JTextArea();
		textArea.setFocusable(false);
		textArea.setEditable(false);
		scrollPane.setViewportView(textArea);

	}

	@Override
	public void doPrint(String text) {
		textArea.append(text + "\r\n");
	}
	
	

}
